/*登陆操作js*/
var layer;//当前页面全局定义 TODO 继续优化
var module = {
    URL: {
        login: "/login",
    },
    /**
     *@description 初始化
     *@author XieXing
     *@date 2020/9/24 13:51
     */
    init: function () {
        layui.use(['layer'], function () {
            layer = layui.layer;
            //点击验证码图片获取验证码
            $("#resCode").click(function () {
                $(this).attr("src", "/getGifCode?" + Math.random());
            });

            /**
             *@description 点击登陆
             *@author XieXing
             *@date 2020/9/24 15:45
             */
            $("#loginBtn").click(function () {
                module.login();
            });

            document.onkeydown = function(e){
                var ev = document.all ? window.event : e;
                if(ev.keyCode==13) {
                    $("#loginBtn").trigger("click");
                }
            };
            //解决iframe跳转到登录页面出现问题
            if (window != top) {
                top.location.href = location.href;
            }
        })
    },
    /**
     *@description 登陆
     *@author XieXing
     *@date 2020/9/24 15:20
     */
    login: function () {
        // var userName = $("#userName").val();//用户名
        var userName = $("#userPhone").val();//用户名用手机号代替
        var userPassword = $("#userPassword").val();//密码
        var code = $("#code").val();
        var verifyRes = module.login_verify(userName, userPassword, code);
        if (!verifyRes.status) {
            $("#resCode").trigger("click");
            layer.msg(verifyRes.msg, {icon: 2, time: 2000});
            return false;
        }
        var params = {userName: userName, userPassword: userPassword, code: code};
        var loading = layer.load(0);
        $.post(module.URL.login, params, function (result) {
            layer.close(loading);
            if (result.code != 200) {
                $("#resCode").trigger("click");
                layer.msg(result.data, {icon: 2})
                return false;
            }
            window.location = "/main";
        })
    },
    /**
     *@description 登录前验证
     *@author XieXing
     *@date 2020/9/24 15:20
     */
    login_verify: function (userName, userPassword, code) {
        var res = {status: true, msg: "验证成功"};
        if (!userName) {
            res.status = false;
            res.msg = "手机号不能为空";
        }
        if (!userPassword) {
            res.status = false;
            res.msg = "密码不能为空";
        }
        if (!code) {
            res.status = false;
            res.msg = "验证码为空";
        }
        return res;
    }
};

module.init();